<?php
/**
 * Created by PhpStorm.
 * User: luzhaoyang
 * Date: 2017/7/21
 * Time: 09:21
 * 海报控制器
 */

class Postimage extends Base_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get()
    {
        $redirect_uri=site_url('/postimage/callback');
        $scope='snsapi_userinfo';
        $this->load->library('openlogin/wechat');
        $this->wechat->set_scope($scope);
        $this->wechat->set_redirect_uri($redirect_uri);
        //跳转到引导页，授权登陆
        $this->wechat->get_code();
    }

    public function callback()
    {
        $this->load->library('openlogin/wechat');
        $this->wechat->get_access_token();
        $info=$this->wechat->get_user_info();
        //检查用户是否已经注册
        $this->load->model('user_model');
        $res=$this->user_model->getUser(array('wx_unionid'=>$info['unionid']),'uid');

        if($info!==false){
            if (empty($res)){
                //新用户注册
                $data = array(
                    'utype'      => 'weixin',
                    'wx_openid'  => $info['openid'],
                    'wx_unionid' => $info['unionid'],
                    'userName'   => $info['nickname'],
                    'userToken'  => md5($info['access_token']),
                    'userIcon'   => $info['userIcon'],
                    'status'     => 1,
                    'created'    => time(),
                    'changed'    => time()
                );
                $uid = $this->user_model->createUser($data);
                if($uid){
                    $this->user_model->createUserInfo(array('uid' => $uid, 'userGender'=>$info['gender']));

                    $this->load->model('redis_model');
                    $redis = $this->redis_model->getRedisInstance();
                    $redis->multi();
                    //设置登陆信息
                    $redis->set('user:'.$info['openid'],$uid);
                    //新用户赠送100能量豆
                    $redis->incrby('user:'.$uid.'.beans',100);
                    $redis->exec();
                }else{die("系统错误，请重试");}
            }else{
                $uid=$res['uid'];
                //用户存在，同步redis数据
                $this->user_model->setUidWithOpenid($info['openid'],$res['uid']);
            }
            //设置登陆session
            if(!isset($_SESSION)) session_start();
            $_SESSION['uid'] = $uid;
            $_SESSION['wx_openid'] = $info['openid'];
            $_SESSION['userName'] = $info['nickname'];
            $_SESSION['userIcon'] = $info['userIcon'];
            redirect('http://wap.nldou.com/postimage/fmdk.html');
        }else{die("系统错误，请重试");}
    }

    /*生成海报*/
    public function fmdk()
    {
        if (!isset($_SESSION)) session_start();
        if (isset($_SESSION['userName'])&&isset($_SESSION['userIcon'])
            &&isset($_SESSION['uid'])&&$_SESSION['wx_openid']){
            $this->load->model('image_model');
            $img=$this->image_model->createFmdkPost($_SESSION['userName'],$_SESSION['userIcon'],$_SESSION['uid']);
            if ($img===false) $this->success(array('status'=>2));
            else $this->success(array('status'=>1,'url'=>$img));
        }else{
            $this->error();
        }
    }
}